package org.sjs.dreamfly.app

import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.children
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import org.sjs.dreamfly.R
import org.sjs.dreamfly.biz.movie.MovieHomeFragment
import org.sjs.dreamfly.common.base.BaseActivity
import org.sjs.dreamfly.common.base.BaseFragment
import org.sjs.dreamfly.biz.news.NewsFragment
import org.sjs.dreamfly.biz.personal.PersonalFragment
import org.sjs.dreamfly.biz.shop.ShopFragment
import org.sjs.dreamfly.biz.shortvideo.ShortVideoFragment
import org.sjs.dreamfly.biz.story.StoryFragment
import org.sjs.dreamfly.common.util.LogUtil

class MainActivity : BaseActivity() {

    private lateinit var viewPager: ViewPager2
    private lateinit var bottomBar: ViewGroup
    private val tabViewList = mutableListOf<View>()

    private var fragmentList: MutableMap<String, BaseFragment> = mutableMapOf()
    private lateinit var viewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        installSplashScreen()
        super.onCreate(savedInstanceState)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (newConfig.orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            hideBottomBar()
        } else {
            showBottomBar()
        }
    }

    override fun onInitViewModel() {
        viewModel = ViewModelProvider(this)[MainViewModel::class.java]
    }

    override fun requireLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun onBindViews() {
        bottomBar = findViewById<ViewGroup>(R.id.bottom_bar)
        viewPager = findViewById(R.id.view_pager)
        tabViewList.addAll(bottomBar.children)
    }

    override fun onInitViews() {
        initBottomBar()
        initViewPager()
        hideTitleBar()
    }

    override fun onBackPressed() {
        if (!fragmentList.toList()[viewPager.currentItem].second.onBackPressed()) {
            super.onBackPressed()
        }
    }

    override fun onLoadData(savedInstanceState: Bundle?, newIntent: Intent?) {
        hideLoadingView()
    }

    private fun initViewPager() {
        viewPager.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int { return viewModel.getTabList().size }
            override fun createFragment(position: Int): Fragment {
                val tab = viewModel.getTabList().toList()[position].first
                return fragmentList[tab]?:(when (tab) {
//                    MainViewModel.TAB_MOVIE -> /**/MovieFragment.newInstance()
                    MainViewModel.TAB_MOVIE -> MovieHomeFragment()
                    MainViewModel.TAB_SHORT_VIDEO -> ShortVideoFragment()
                    MainViewModel.TAB_STORY -> StoryFragment.newInstance()
                    MainViewModel.TAB_SHOP -> ShopFragment.newInstance()
                    MainViewModel.TAB_NEWS -> NewsFragment.newInstance()
                    else -> PersonalFragment.newInstance()
                }.apply {
                    fragmentList[tab] = this
                })
            }
        }
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                tabViewList.forEachIndexed { index, tab ->
                    val tabType = ((tab as ViewGroup)[1] as TextView).text
                    if (index == position) {
                        ((tab as ViewGroup)[0] as ImageView).setImageResource(viewModel.getTabList()[tabType]!!.first)
                        (tab[1] as TextView).setTextColor(ResourcesCompat.getColor(resources,
                            R.color.primary, null))
                    } else {
                        ((tab as ViewGroup)[0] as ImageView).setImageResource(viewModel.getTabList()[tabType]!!.second)
                        (tab[1] as TextView).setTextColor(ResourcesCompat.getColor(resources,
                            R.color.primary_gray, null))
                    }
                }
            }
        })
        viewPager.isUserInputEnabled = false
    }

    private fun initBottomBar() {
        viewModel.getTabList().toList().forEachIndexed { index, pair ->
            ((tabViewList[index] as ViewGroup)[1] as TextView).text = pair.first
        }
        tabViewList.forEachIndexed { index, view ->
            view.setOnClickListener {tab ->
                val tabType = ((tab as ViewGroup)[1] as TextView).text
                LogUtil.d("onTabSelected:$tabType")
                viewPager.setCurrentItem(index, true)
            }
        }
    }

    private fun hideBottomBar() {
        bottomBar.visibility = View.GONE
    }

    private fun showBottomBar() {
        bottomBar.visibility = View.VISIBLE
    }

    fun switchToHomeTab() {
        tabViewList[1].performClick()
    }
}

